<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Request;

class Comment extends Model
{
	/* 添加评论API */
    public function add()
    {
    	/* 检查用户是否登录 */
    	if (!user_ins()->is_logged_in())
    		return ['status' => 0, 'msg' => 'Login Required'];

    	/* 检查评论内容是否为空 */
    	if (!Request::get('content'))
    		return ['status' => 0, 'msg' => 'Empty Content'];

    	/* 仅能传递question_id或者answer的id中的一个参数 */
    	if (
    			(!Request::get('question_id')) && (!Request::get('answer_id')) || 
    			( Request::get('question_id')) && (Request::get('answer_id'))
    		 )
    		return ['status' => 0, 'msg' => 'Question_id or Answer_id only Required One'];

    	/* 判断参数是question_id还是id,并实例化Model后进行两分支的逻辑代码书写 */
    	if (Request::get('question_id'))
    	{
            // 使用实例,查找数据库中该question_id
    		$question = question_ins()->find(Request::get('question_id'));

    		/* 如果comments数据表中没有这个question,则返回错误 */
            if (!$question)
                return ['status' => 0, 'msg' => 'Question Not Exists'];
            /* 如果有,则进行重新赋值操作 */
            $this->question_id = Request::get('question_id');
    	} else {
            // 使用实例,查找数据库中该answer_id
    		$answer = answer_ins()->find(Request::get('answer_id'));

			/* 如果数据库中没有这个answer_id,则返回错误 */
    		if (!$answer)
    			return ['status' => 0, 'msg' => 'Answer Not Exists'];
    		/* 如果有,则进行重新赋值操作 */
    		$this->answer_id = Request::get('answer_id');
    	}

    	/* 检查是否存在reply_to参数,对谁评论? */
    	if (Request::get('reply_to'))
    	{
    		/* 获取评论对象 */
    		$targetObj = $this->find(Request::get('reply_to'));
    		/* 评论对象不存在 */
    		if (!$targetObj)
    			return ['status' => 0, 'msg' => 'TargetObj Comment Not Exists'];
    		/* 评论对象存在,不能自己的评论 */
            if ($targetObj->user_id == session('user_id'))
                return ['status' => 0, 'msg' => 'Can Not Reply Youself'];
    		$this->reply_to = Request::get('reply_to');
    	}

    	$this->content = Request::get('content');
    	$this->user_id = session('user_id');
    	return $this->save()?
    	['status' => 1, 'id' => $this->id]:
    	['status' => 0, 'msg' => 'DB Insert Failed'];
    }

    /* 删除评论API */
    public function remove()
    {
        /* 检查用户是否登录 */
        if (!user_ins()->is_logged_in())
            return ['status' => 0, 'msg' => 'Login Required'];
        /* 传递需要被删除的评论的id */
        if (!Request::get('id'))
            return ['status' => 0, 'msg' => 'Comment id Required'];
        /* 在数据库中查找与参数id相同的评论数据 */
        $comment = $this->find(Request::get('id'));
        if (!$comment)
            return ['status' => 0, 'msg' => 'Comment Not Exists'];
        /* 评论存在，只有评论所有者可以删除 */
        if ($comment->user_id != session('user_id'))
            return ['status' => 0, 'msg' => 'Permission Denied'];

        /* 存在外键约束,需要先删除该评论下所有回复评论 */
        $this->where('reply_to', Request::get('id')->delete());

        return $comment->delete()?
        ['status' => 1]:
        ['status' => 0, 'msg' => 'Permission Denied'];
    }

    /* 查看评论API */
    public function read()
    {
    	/* 检查用户是否登录 */
        if (!user_ins()->is_logged_in())
            return ['status' => 0, 'msg' => 'Login Required'];
        /* 必须传递question_id或者answer_id其中一个 */
        if (!Request::get('question_id') && !Request::get('answer_id'))
            return ['status' => 0, 'msg' => 'Question_id and Answer_id Require One'];

        /* 检查是否发送了question_id */
        if (Request::get('question_id'))
        {
            $question = question_ins()->find(Request::get('question_id'));
            if (!$question)
                return ['status' => 0, 'msg' => 'Question Not Exists'];
            $data = $this->where('question_id', Request::get('question_id'));
        }
        else 
        {
            $answer = answer_ins()->find(Request::get('answer_id'));
            if (!$answer)
                return ['status' => 0, 'msg' => 'Answer Not Exists'];
            $data = $this->where('answer_id', Request::get('answer_id'));
        }
        $data = $data->get()->keyBy('id');
        return ['status' => 0, 'msg' => $data];
    }
}
